#include "timedtaskmanagerapi.h"

TimedTaskManagerApi::TimedTaskManagerApi(QObject *parent)
    : QObject{parent}
{
    baseUrl = Config::baseUrl;
    manager = new QNetworkAccessManager(0);
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));//post请求设置为json，才能发送josn格式文件
    request.setRawHeader("Authorization",  qApp->property("token").value<QString>().toLocal8Bit());
}

QByteArray *TimedTaskManagerApi::list(int pageNum,int pageSize,QString jobName,
                                      QString jobGroup,QString status)
{
    QString params = "monitor/job/list?pageNum="+QString::number(pageNum)+
                     "&pageSize="+QString::number(pageSize);
    if(!jobName.isEmpty()){
        params += "&jobName=" + jobName;
    }
    if(!jobGroup.isEmpty()){
        params += "&jobGroup=" + jobGroup;
    }
    if(!status.isEmpty()){
        params += "&status=" + status;
    }
    request.setUrl(baseUrl + params);
    QNetworkReply *reply= manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *TimedTaskManagerApi::editJob(Job &job)
{
    request.setUrl(baseUrl + "monitor/job");
    QJsonObject obj = job.toJson();
    QByteArray arr = QJsonDocument(obj).toJson();
    QNetworkReply *reply = job.jobId > 0 ? manager->put(request,arr) : manager->post(request,arr);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *TimedTaskManagerApi::deleteItems(QString ids)
{
    request.setUrl(baseUrl + "monitor/job/" + ids);
    QNetworkReply * r = manager->deleteResource(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *TimedTaskManagerApi::getById(qlonglong id)
{
    request.setUrl(baseUrl + "monitor/job/" + QString::number(id));
    QNetworkReply * r = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *TimedTaskManagerApi::changeStatus(QString id, QString status)
{
    request.setUrl(baseUrl + "monitor/job/changeStatus");
    QJsonObject obj;
    obj.insert("jobId",id);
    obj.insert("status",status);
    QByteArray arr = QJsonDocument(obj).toJson();
    QNetworkReply * r = manager->put(request,arr);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *TimedTaskManagerApi::run(QString id, QString group)
{
    request.setUrl(baseUrl + "monitor/job/run");
    QJsonObject obj;
    obj.insert("jobId",id);
    obj.insert("group",group);
    qDebug()<<obj;
    QByteArray arr = QJsonDocument(obj).toJson();
    QNetworkReply * r = manager->put(request,arr);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    qDebug()<<data->data();
    return data;
}

QByteArray *TimedTaskManagerApi::logList(int pageNum, int pageSize, QString jobName, QString jobGroup,
                                         QString status,QString start,QString end)
{
    QString params = "monitor/jobLog/list?pageNum="+QString::number(pageNum)+
                     "&pageSize="+QString::number(pageSize);
    if(!jobName.isEmpty()){
        params += "&jobName=" + jobName;
    }
    if(!jobGroup.isEmpty()){
        params += "&jobGroup=" + jobGroup;
    }
    if(!status.isEmpty()){
        params += "&status=" + status;
    }
    if(start != "2000/1/1" && end != "2000/1/1"){
        params += "&params%5BbeginTime%5D=" + start + "&params%5BendTime%5D=" + end;
    }
    request.setUrl(baseUrl + params);
    QNetworkReply *reply= manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *TimedTaskManagerApi::deleteLogItems(QString ids)
{
    request.setUrl(baseUrl + "monitor/jobLog/" + ids);
    QNetworkReply * r = manager->deleteResource(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *TimedTaskManagerApi::clean()
{
    request.setUrl(baseUrl + "monitor/jobLog/clean");
    QNetworkReply * r = manager->deleteResource(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}
